<?php
/**
 *
 * User:xinhuazhang
 * Date:2024/7/18
 * TIPS:功能说明
 * Email:<1655626823@qq.com>
 */

namespace app\api\controller;

use app\admin\model\Recharge;
use app\admin\model\Withdraw;
use app\common\controller\Frontend;
use think\Exception;

#[Apidoc\Title("我的钱包--->新版本3.0")]
class Mymoney extends Frontend
{
    protected array $noNeedPermission = ['*'];

    /**
     * 我的赏金
     *
     */
    #[
        Apidoc\Title("我的钱包3.0"),
        Apidoc\Tag("action income => '我的收款', expend => '我的付款'"),
        Apidoc\Method("POST"),
        Apdoc\version("2.0"),
        Apidoc\Url("/api/mymoney/index"),
        Apidoc\Query(name: "action", type: "string", require: true, desc: "顶部tab expend我的付款 income我的收款", mock: "income"),
        Apidoc\Param(name: "action", type: "string", require: true, desc: "顶部tab expend我的付款 income我的收款", mock: "income"),
        Apidoc\Query(name: "page", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Returned("id", type: "int", desc: "Id 提现时候需要"),
        Apidoc\Returned("name", type: "string", desc: "标题（房源和找店名称）"),
        Apidoc\Returned("house_type", type: "string", desc: "特别标识    不等于is_vip是单次看房 否则就是vip会员费 其它字段参照原来佣金列表的  "),
//        Apidoc\Returned("check_text", type: "string", desc: "我的收款状态 check_status  0 => '待提现',
//        1 => '待审核',
//        2 => '已支付',
//        3 => '已驳回',"),
//        Apidoc\Returned("pay_text", type: "string", desc: "我的付款状态 check_status等于2 => '已支付' 其它都是支付中"),
//        Apidoc\Returned("order_amount", type: "number", desc: "悬赏佣金"),
//        Apidoc\Returned("house_type", type: "string", desc: "houses房源，findstore 找房类型 对应的house_id"),
//
//        Apidoc\Returned("truename", type: "string", desc: "收款人"),
//        Apidoc\Returned("bankno", type: "string", desc: "银行卡号"),
//        Apidoc\Returned("open_bank", type: "string", desc: "开户行"),
//        Apidoc\Returned("check_msg", type: "string", desc: "审核信息"),
    ]
    public function index()
    {

    }

    #[
        Apidoc\Method("POST"),
        Apdoc\version("3.0"),
        Apidoc\Title("提现提交3.0"),
        Apidoc\Url("/api/mymoney/withdraw"),
        Apidoc\Query("truename", type: "string", desc: "收款人"),
        Apidoc\Query("money", type: "string", desc: "提现金额"),
        Apidoc\Query("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Query("open_bank", type: "string", desc: "开户行"),

        Apidoc\Param("truename", type: "string", desc: "收款人"),
        Apidoc\Param("money", type: "string", desc: "提现金额"),
        Apidoc\Param("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Param("open_bank", type: "string", desc: "开户行")
    ]
    /*
     * 提现提交方法
    */
    public function withdraw()
    {
        $money = $this->request->param('money');
        if (empty($money)) {
            $this->error('提现金额不能为空');
        }
        try {
            $withdra = new Withdraw();
            $withdramoney = $withdra
                ->where([
                    'user_id' => $this->auth->id,
                    'type' => 'sub',
                    'status' => 1
                ])
                ->sum('money');//体现中的钱

            $noinfo = (new \app\admin\model\User())->where('id', '=', $this->auth->id)->find();
            $last_money = $noinfo['money'] - $noinfo['unmoney'];
            if ($money > $last_money) {
                $this->error('提现金额不能大于可用余额' . $last_money);
            }
            (new Withdraw())->startTrans();
            (new Withdraw())->insert([
                'money' => $money,
                'type' => 'sub',
                'memo' => '提现金额' . ($money),
                'user_id' => $this->auth->id,
                'status' => 1,
                'check_status' => 1,
                'create_time' => time(),
            ]);
            $noinfo->save([
                'unmoney' => $money + $noinfo['unmoney']
            ]);
            (new Withdraw())->commit();
            //(new \app\common\model\User())->money($money, $this->auth->id, '提现金额：' . $money, 'sub');
            $this->success('提现提交成功，等待审核通过');
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }

    }

    /*提现记录
    */
    #[
        Apidoc\Method("POST"),
        Apdoc\version("3.0"),
        Apidoc\Title("提现记录3.0 ,check_status 等于1=>'待审核',2'已支付',3'驳回'"),
        Apidoc\Url("/api/mymoney/drawhis"),
    ]
    public function drawhis()
    {
        $map = [];
//        $type = $this->request->param('type', 'add');
//        if (!empty($type)) {
//            $map['type'] = $type;
//        }
        $li = (new Withdraw())->where($map)
            ->field('id,check_status,create_time,money,checktime,bankno,open_bank,truename,check_img')
            ->where(['user_id' => $this->auth->id])
            ->order('id', 'desc')
            ->paginate()->each(function ($item) {
                $item['createtime'] = date("Y-m-d H:is", $item['createtime']);
            });

        $result = [
            'current_page' => $li->currentPage(),
            'data' => $li->items(),
            'last_page' => $li->lastPage(),
            'per_page' => $li->listRows(),
            'total' => $li->total(),
            'userinfo' => $this->auth->getUserInfo()
        ];

        $this->success('返回成功', $result);
    }


    /**
     * 充值提交
     */
    public function recharge()
    {
        (new Recharge())->save([
            'user_id' => $this->auth->id,
            'money' => $this->request->param('money'),
            'status' => 'opt0'
        ]);
        $this->success('提交成功，等待审核');
    }
    public function chargehis()
    {
        $list=(new Recharge())
            ->where([
               'user_id' => $this->auth->id
            ])
            ->paginate();
        $this->success('ok',$list);
    }
}
